﻿using UnityEngine;
using UnityEngine.UI;
using GX;
using Component = UnityEngine.Component;
using BasePanel = GX.BasePanel;
public abstract class Wnd : BasePanel
{
    static GameObject _uiRoot;

    static GameObject uiRoot
    {
        get
        {
            if (null == _uiRoot)
                _uiRoot = GameObject.Find("UIRoot");
            return _uiRoot;
        }
    }
    IModule _module;

    GameObject _prefab;

    GameObject _go;
    Canvas _canvas;
    GraphicRaycaster _raycaster;

    public GameObject gameObject { get { return _go; } }
    public bool isVisible { get { return null != _go && _go.activeSelf; } }
    virtual public int moduleID { get { return 0; } }
    public IModule module
    {
        get
        {
            if (null == _module && moduleID != 0)
                _module = ModuleManager.GetModule(moduleID);
            return _module;
        }
    }
    public T AddComponent<T>() where T : UnityEngine.Component
    {
        var t = _go.AddComponent<T>();
        return t;
    }
    public T FindComponent<T>(string name = null, bool isRecursively = false, bool includeDeactive = true) where T : Component
    {
        T t = _go.FindComponent<T>(name, isRecursively, includeDeactive);
        return t;
    }
    public GameObject FindChild(string name, bool isRecursively = false, bool includeDeactive = true)
    {
        var go = _go.FindGameObject(name, isRecursively, includeDeactive);
        return go;
    }
    override public void SetVisible(bool v)
    {
        if (null != _go)
            _go.SetActive(v);
    }
    override protected void StartLoad()
    {
        AssetManager.Load(uiCO.path, OnUILoaded, null);
    }
    void OnUILoaded(IAssetInfo info)
    {
        var obj = info.GetAsset();
        _prefab = obj as GameObject;

        this.FinishLoad();
    }
    override protected bool CreateUI()
    {
        if (null == _go)
        {
            var layerRoot = uiRoot.transform.GetChild(this.layer - 1);

            _go = GameObject.Instantiate(_prefab);
            _go.name = this.name;
            _go.transform.SetParent(layerRoot.transform, false);

            _canvas = _go.AddComponent<Canvas>();
            _canvas.overrideSorting = true;

            _raycaster = _go.AddComponent<GraphicRaycaster>();
            _raycaster.enabled = this.isOpen;

            if (null != _canvas)
            {
                _canvas.sortingOrder = this.sortOrder;
            }
            _go.SetActive(this.isOpen);
            OnCreate();
        }
        return true;
    }

    protected override void UpdateOrder()
    {
        if (null != _canvas)
        {
            _canvas.sortingOrder = this.sortOrder;
        }
    }
    virtual protected void OnCreate()
    {
        Debug.Log("【" + this.name + "】" + "UIPage.OnCreate");
    }
    override protected void OnEnter(ArgBundle args)
    {
        Debug.Log("【" + this.name + "】" + "UIPage.OnEnter");
    }
    override protected void OnResume()
    {
        Debug.Log("【" + this.name + "】" + "UIPage.OnResume");
    }
    override protected void OnPause()
    {
        Debug.Log("【" + this.name + "】" + "UIPage.OnPause");
    }
    override protected void OnExit()
    {
        Debug.Log("【" + this.name + "】" + "UIPage.OnExit");
    }
    override protected void OnDestroy()
    {
        Debug.Log("【" + this.name + "】" + "UIPage.OnDestroy");
    }
}